home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / progsrc / mkfnt8 / mkfnt8.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-13  |  14.4 KB  |  575 lines

  1. #include <stdio.h>
  2. #include <graph.h>
  3. #include <conio.h>
  4. #include <malloc.h>
  5. #include <string.h>
  6. #include <dos.h>
  7.  
  8. #define byte unsigned char
  9. #define word unsigned int
  10.  
  11. #define OFF 0
  12. #define ON 1
  13.  
  14. #define FAILURE 0
  15. #define SUCCESS 1
  16.  
  17. #define goto(x,y) _settextposition(y,x)
  18.  
  19. char path[]={""};
  20.  
  21. #include "font.h"
  22. #include "graphics.h"
  23. #include "palette.h"
  24. #include "text.h"
  25. #include "mouse.h"
  26.  
  27. #define MAX_TILES 256
  28. #define X_MAX 8
  29. #define Y_MAX 8
  30.  
  31. /****************************************************************************
  32. function prototypes
  33. ****************************************************************************/
  34.  
  35. void set_screen(void);
  36. byte allocate_memory(void);
  37. void free_memory(void);
  38.  
  39. void init_mouse(void);
  40.  
  41. void refresh_picture(byte, byte);
  42. void refresh_char_map(byte);
  43.  
  44. byte save(char *);
  45. byte load(char *);
  46.  
  47. void insert(void);
  48. void delete(void);
  49.  
  50. void update_color_info(byte);
  51.  
  52. /****************************************************************************
  53. global variables
  54. ****************************************************************************/
  55.  
  56. byte huge *screen=(byte huge *)0xA0000000;
  57.  
  58. char palette_file[]={"mode13h"};
  59. char font_file[]={"fnt8x8"};
  60.  
  61. byte xor[X_MAX]={1,2,4,8,16,32,64,128};
  62.  
  63. /****************************************************************************/
  64.  
  65. void main(void)
  66. {
  67.  int x=0, y=0;
  68.  byte current_font=0, current_color=7;
  69.  int a, b;
  70.  int key=0;
  71.  int tmp;
  72.  
  73. /****************************************************************************/
  74.  
  75.  if (allocate_memory()==FAILURE) goto END;
  76.  
  77.  if (read_font(font_file)==FAILURE)
  78.   goto END;
  79.  
  80.  set_screen();
  81.  
  82.  if (read_palette(palette_file)==SUCCESS)
  83.   set_palette();
  84.  else getch();
  85.  
  86.  
  87.  init_mouse();
  88.  
  89. /****************************************************************************/
  90. /****************************************************************************/
  91.  
  92.  do
  93.  {
  94.   put_string(169,0,"   ",0,ERASE);
  95.   put_number_i(169,0,(int)current_font,7,ERASE);
  96.   put_number_i(145,8,(int)x,7,ERASE);
  97.   put_number_i(185,8,(int)y,7,ERASE);
  98.   put_string(80,192,"   ",0,ERASE);
  99.   put_number_i(80,192,(int)current_color,7,ERASE);
  100.  
  101.   update_color_info(current_color);
  102.  
  103.   refresh_picture(current_font,current_color);
  104.   _setcolor(7);
  105.   _rectangle(_GBORDER,x*16+1,y*16+1,x*16+14,y*16+14);
  106.   _rectangle(_GBORDER,(current_color-(current_color/32)*32)*4,
  107.               current_color/32*8+128,
  108.               (current_color-(current_color/32)*32)*4+3,
  109.               current_color/32*8+128+7);
  110.  
  111.   refresh_mouse();
  112.  
  113. /****************************************************************************/
  114.   while (!kbhit())
  115.   {
  116.    palette_switch(252);
  117.  
  118.    update_color_info(current_color);
  119.  
  120.    hide_mouse();
  121.    read_mouse();
  122.  
  123.    put_font((current_font-(current_font/16)*16)*8+192,current_font/16*8,
  124.         current_font,7,INVERSE);
  125.  
  126. /****************************************************************************/
  127.  
  128.  
  129.     if (mouse_1)
  130.     {
  131.  
  132.      refresh_picture(current_font,current_color);
  133.      _setcolor(current_color);
  134.      _rectangle(_GBORDER,(current_color-(current_color/32)*32)*4,
  135.              current_color/32*8+128,
  136.              (current_color-(current_color/32)*32)*4+3,
  137.              current_color/32*8+128+7);
  138.      if (mouse_x<128)
  139.      {
  140.       if (mouse_y>127 && mouse_y<192)
  141.       {
  142.        current_color=(mouse_x/4)+((mouse_y-128)/8)*32;
  143.        refresh_picture(current_font,current_color);
  144.        refresh_char_map(current_color);
  145.       }
  146.       else if (mouse_y<128)
  147.       {
  148.        font[current_font][mouse_y/16]=
  149.     font[current_font][mouse_y/16]^xor[mouse_x/16];
  150.        refresh_picture(current_font,current_color);
  151.       }
  152.      }
  153.      else if (mouse_x>=192)
  154.      {
  155.       if (mouse_y<128)
  156.       {
  157.        current_font=(mouse_x-192)/8+(mouse_y/8*16);
  158.       }
  159.      }
  160.      else
  161.      {
  162.       if (mouse_y>=64 && mouse_y<72)
  163.       {
  164.        save(font_file);
  165.       }
  166.       else if (mouse_y>=72 && mouse_y<80)
  167.       {
  168.        load(font_file);refresh_char_map(current_color);
  169.        refresh_picture(current_font,current_color);
  170.       }
  171.       else if (mouse_y>=80 && mouse_y<88)
  172.       {
  173.        write_palette(palette_file);
  174.       }
  175.       else if (mouse_y>=88 && mouse_y<96)
  176.       {
  177.        read_palette(palette_file);
  178.        set_palette();
  179.       }
  180.       else if (mouse_x>=138 && mouse_x<144 && mouse_y>132 && mouse_y<=195)
  181.       {
  182.        palette[current_color*3+0]=195-mouse_y;
  183.        set_palette();
  184.       }
  185.       else if (mouse_x>=154 && mouse_x<160 && mouse_y>132 && mouse_y<=195)
  186.       {
  187.        palette[current_color*3+1]=195-mouse_y;
  188.        set_palette();
  189.       }
  190.       else if (mouse_x>=170 && mouse_x<176 && mouse_y>132 && mouse_y<=195)
  191.       {
  192.        palette[current_color*3+2]=195-mouse_y;
  193.        set_palette();
  194.       }
  195.      }
  196.      _setcolor(7);
  197.      _rectangle(_GBORDER,x*16+1,y*16+1,x*16+14,y*16+14);
  198.      _rectangle(_GBORDER,(current_color-(current_color/32)*32)*4,
  199.              current_color/32*8+128,
  200.              (current_color-(current_color/32)*32)*4+3,
  201.              current_color/32*8+128+7);
  202.  
  203.      put_string(169,0,"   ",0,ERASE);
  204.      put_number_i(169,0,(int)current_font,7,ERASE);
  205.      put_number_i(145,8,(int)x,7,ERASE);
  206.      put_number_i(185,8,(int)y,7,ERASE);
  207.      put_string(80,192,"   ",0,ERASE);
  208.      put_number_i(80,192,(int)current_color,7,ERASE);
  209.     }
  210.  
  211. /****************************************************************************/
  212.    update_mouse();
  213.    put_font((current_font-(current_font/16)*16)*8+192,current_font/16*8,
  214.         current_font,0,NORMAL);
  215.  
  216.   }
  217.  
  218. /****************************************************************************/
  219.   hide_mouse();
  220.  
  221.   refresh_picture(current_font,current_color);
  222.   _setcolor(current_color);
  223.   _rectangle(_GBORDER,(current_color-(current_color/32)*32)*4,
  224.               current_color/32*8+128,
  225.               (current_color-(current_color/32)*32)*4+3,
  226.               current_color/32*8+128+7);
  227.  
  228.   key=getch();
  229.   if (key==0)
  230.   {
  231.    key=getch();
  232.    switch (key)
  233.    {
  234.     case 0x48: y--;break;
  235.     case 0x50: y++;break;
  236.     case 0x4b: x--;break;
  237.     case 0x4d: x++;break;
  238.    }
  239.    _setcolor(0);
  240.    for (a=0;a<3;a++)
  241.    {
  242.     tmp=(byte)(current_color);
  243.  
  244.    }
  245.   }
  246.   else
  247.   {
  248.    switch (key)
  249.    {
  250.     case ' ': font[current_font][y]=font[current_font][y]^xor[x];break;
  251.     case '=':
  252.     case '+': refresh_char_map(++current_color);break;
  253.     case '-': refresh_char_map(--current_color);break;
  254.     case '.':
  255.     case '>': current_font++;
  256.           break;
  257.     case ',':
  258.     case '<': current_font--;
  259.           break;
  260.     case 's': save(font_file);break;
  261.     case 'l': load(font_file);refresh_char_map(current_color);
  262.           refresh_picture(current_font,current_color);break;
  263.     case 'p': write_palette(palette_file);break;
  264.     case 'g': read_palette(palette_file);set_palette();break;
  265.     case 'i': for (b=255;b>current_font;b--)
  266.           {
  267.            for (a=0;a<8;a++)
  268.            {
  269.         font[b][a]=font[b-1][a];
  270.            }
  271.           }
  272.           for (a=0;a<8;a++)
  273.            font[current_font][a]=0;
  274.           refresh_char_map(current_color);
  275.           refresh_picture(current_font,current_color);break;
  276.     case 'd': for (b=current_font;b<255;b++)
  277.           {
  278.           for (a=0;a<8;a++)
  279.            font[b][a]=font[b+1][a];
  280.           }
  281.           for (a=0;a<8;a++)
  282.            font[255][a]=0;
  283.           refresh_char_map(current_color);
  284.           refresh_picture(current_font,current_color);break;
  285.    }
  286.   }
  287.  
  288.   if (y<0) y=Y_MAX-1;
  289.   else if (y>=Y_MAX) y=0;
  290.   if (x<0) x=X_MAX-1;
  291.   else if (x>=X_MAX) x=0;
  292.  
  293.  } while (key!=27);
  294.  
  295. /****************************************************************************/
  296.  
  297.  _setvideomode(_DEFAULTMODE);
  298.  
  299. /****************************************************************************/
  300. END:;
  301. free_memory();
  302.  
  303. }
  304.  
  305. /****************************************************************************/
  306. /****************************************************************************/
  307. /****************************************************************************/
  308.  
  309. void set_screen(void)
  310. {
  311.  int a, b;
  312.  
  313.  _setvideomode(_MRES256COLOR);
  314.  
  315.  _setcolor(4);
  316.  _rectangle(_GFILLINTERIOR,0,0,127,127);
  317.  
  318.  for (b=0;b<8;b++)
  319.  {
  320.   for (a=0;a<32;a++)
  321.   {
  322.    _setcolor(b*32+a);
  323.    _rectangle(_GFILLINTERIOR,a*4,b*8+128,a*4+3,b*8+128+7);
  324.   }
  325.  }
  326.  
  327.  _setcolor(0);
  328.  
  329.  for (b=0;b<Y_MAX;b++)
  330.  {
  331.   for (a=0;a<X_MAX;a++)
  332.   {
  333.    _rectangle(_GFILLINTERIOR,a*16+1,b*16+1,a*16+14,b*16+14);
  334.   }
  335.  }
  336.  
  337. /****************************************************************************/
  338. /* setup palette-information bar */
  339. /****************************************************************************/
  340.  
  341.  _setcolor(4);
  342.  _rectangle(_GFILLINTERIOR,128,128,128+56,199);
  343.  
  344.  for (a=0;a<3;a++)
  345.  {
  346.   _setcolor(0);
  347.   _rectangle(_GFILLINTERIOR,139+a*16,132,145+a*16,195);
  348.   _setcolor(7);
  349.   _rectangle(_GBORDER,137+a*16,131,145+a*16,196);
  350.  }
  351.  
  352. /****************************************************************************/
  353.  
  354.  for (b=0;b<16;b++)
  355.  {
  356.   for (a=0;a<16;a++)
  357.   {
  358.    put_font(a*8+192,b*8,(byte)(b*16+a),7,NORMAL);
  359.   }
  360.  }
  361.  
  362. /****************************************************************************/
  363.  
  364.  put_string(129,0,"font:",7,NORMAL);
  365.  put_string(129,64,"[S]ave",7,NORMAL);
  366.  put_string(129,72,"[L]oad",7,NORMAL);
  367.  put_string(129,80,"Put Pal",7,NORMAL);
  368.  put_string(129,88,"Get Pal",7,NORMAL);
  369.  put_string(129,8,"x:  y:",7,NORMAL);
  370.  put_string(8,192,"color is:",7,NORMAL);
  371.  put_string(192,128,"total file space",7,NORMAL);
  372.  put_string(240,136,"2048 bytes",7,NORMAL);
  373.  put_string(200,144,"filename",7,NORMAL);
  374.  put_string(256,152,font_file,7,NORMAL);
  375.  put_string(192,160,"palette file",7,NORMAL);
  376.  put_string(256,168,palette_file,7,NORMAL);
  377.  
  378. }
  379.  
  380. /****************************************************************************/
  381. /****************************************************************************/
  382. /****************************************************************************/
  383.  
  384. byte allocate_memory(void)
  385. {
  386.  int a, b;
  387.  
  388.  font=(byte huge **)halloc((long)MAX_TILES,4);
  389.  if (font==NULL)
  390.  {
  391.   printf("error allocating font pointers\n");
  392.   return(FAILURE);
  393.  }
  394.  
  395.  for (a=0;a<MAX_TILES;a++)
  396.  {
  397.   font[a]=(byte huge *)halloc((long)(8),1);
  398.   if (font[a]==NULL) return(FAILURE);
  399.  }
  400.  
  401.  mouse_cursor=(byte huge *)halloc((long)MOUSE_CURSOR_SIZE,1);
  402.  mouse_background=(byte huge *)halloc((long)MOUSE_CURSOR_SIZE,1);
  403.  
  404.  return(SUCCESS);
  405. }
  406.  
  407. /****************************************************************************/
  408. /****************************************************************************/
  409. /****************************************************************************/
  410.  
  411. void free_memory(void)
  412. {
  413.  int a, b;
  414.  
  415.  for (a=0;a<MAX_TILES;a++)
  416.   if (font[a]!=NULL) hfree(font[a]);
  417.  if (font!=NULL) hfree(font);
  418.  
  419.  if (mouse_cursor!=NULL) hfree(mouse_cursor);
  420.  if (mouse_background!=NULL) hfree(mouse_background);
  421. }
  422.  
  423. /****************************************************************************/
  424. /****************************************************************************/
  425. /****************************************************************************/
  426.  
  427. void init_mouse(void)
  428. {
  429.  byte tmp[]={1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  430.          1,7,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
  431.          1,7,7,1,0,0,0,0,0,0,0,0,0,0,0,0,
  432.          1,7,7,7,1,0,0,0,0,0,0,0,0,0,0,0,
  433.          1,7,1,7,7,1,0,0,0,0,0,0,0,0,0,0,
  434.          1,7,1,1,7,7,1,0,0,0,0,0,0,0,0,0,
  435.          1,7,1,0,1,7,7,1,0,0,0,0,0,0,0,0,
  436.          1,7,1,0,0,1,7,7,1,0,0,0,0,0,0,0,
  437.          1,7,1,0,0,0,1,7,7,1,0,0,0,0,0,0,
  438.          1,7,1,0,0,1,1,1,7,7,1,0,0,0,0,0,
  439.          1,7,1,0,1,7,7,7,7,7,7,1,0,0,0,0,
  440.          1,7,1,1,7,1,1,1,1,1,1,0,0,0,0,0,
  441.          1,7,1,7,1,0,0,0,0,0,0,0,0,0,0,0,
  442.          1,7,7,1,0,0,0,0,0,0,0,0,0,0,0,0,
  443.          1,7,1,0,0,0,0,0,0,0,0,0,0,0,0,0,
  444.          1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
  445.  memcpy(mouse_cursor,tmp,256);
  446.  read_mouse();
  447. }
  448.  
  449. /****************************************************************************/
  450. /****************************************************************************/
  451. /****************************************************************************/
  452.  
  453. void refresh_picture(byte current_font, byte current_color)
  454. {
  455.  int a, b, x, y;
  456.  byte tmp;
  457.  
  458.  x=(current_font-(current_font/16)*16)*8+192;
  459.  y=current_font/16*8;
  460.  
  461.  _setcolor(0);
  462.  _rectangle(_GFILLINTERIOR,x,y,x+7,y+7);
  463.  put_font(x,y,current_font,current_color,NORMAL);
  464.  
  465.  for (b=0;b<8;b++)
  466.  {
  467.   for (a=0;a<8;a++)
  468.   {
  469.    if ((tmp=font[current_font][b]&xor[a]))
  470.     _setcolor(current_color);
  471.    else
  472.     _setcolor(0);
  473.  
  474.    _rectangle(_GFILLINTERIOR,a*16+1,b*16+1,a*16+14,b*16+14);
  475.   }
  476.  }
  477. }
  478.  
  479. /****************************************************************************/
  480. /****************************************************************************/
  481. /****************************************************************************/
  482.  
  483. byte load(char *filename)
  484. {
  485.  char buf[80];
  486.  size_t itemsread;
  487.  FILE *file;
  488.  int a, b;
  489.  
  490.  sprintf(buf,"%s%s.fnt",path,filename);
  491.  
  492.  if ((file=fopen(buf,"rb"))==NULL)
  493.  {
  494.   printf("Unable to open %s for read.\n",buf);
  495.   return (FAILURE);
  496.  }
  497.  for (a=0;a<256;a++)
  498.   itemsread=fread(font[a],8,1,file);
  499.  fclose(file);
  500.  
  501.  return(SUCCESS);
  502. }
  503.  
  504. /****************************************************************************/
  505. /****************************************************************************/
  506. /****************************************************************************/
  507.  
  508. byte save(char *filename)
  509. {
  510.  char buf[80];
  511.  size_t items_written;
  512.  FILE *file;
  513.  int a;
  514.  
  515.  sprintf(buf,"%s%s.fnt",path,filename);
  516.  
  517.  if ((file=fopen(buf,"wb"))==NULL)
  518.  {
  519.   printf("Unable to open %s for write.\n",buf);
  520.   return (FAILURE);
  521.  }
  522.  for (a=0;a<256;a++)
  523.   items_written=fwrite(font[a],8,1,file);
  524.  fclose(file);
  525.  
  526.  return(SUCCESS);
  527. }
  528.  
  529. /****************************************************************************/
  530. /****************************************************************************/
  531. /****************************************************************************/
  532.  
  533. void refresh_char_map(byte current_color)
  534. {
  535.  int a, b;
  536.  
  537.  for (b=0;b<16;b++)
  538.  {
  539.   for (a=0;a<16;a++)
  540.   {
  541.    put_font(a*8+192,b*8,(byte)(b*16+a),current_color,ERASE);
  542.   }
  543.  }
  544. }
  545.  
  546. /****************************************************************************/
  547. /****************************************************************************/
  548. /****************************************************************************/
  549.  
  550. void insert(void)
  551. {
  552.  
  553. }
  554.  
  555. /****************************************************************************/
  556. /****************************************************************************/
  557. /****************************************************************************/
  558.  
  559. void update_color_info(byte color)
  560. {
  561.  int a, b;
  562.  
  563.  for (b=0;b<3;b++)
  564.  {
  565.   _setcolor(b*8+15);
  566.   _rectangle(_GFILLINTERIOR,138+b*16,195,144+b*16,195-palette[color*3+b]);
  567.   _setcolor(0);
  568.   _rectangle(_GFILLINTERIOR,138+b*16,195-palette[color*3+b]-1,144+b*16,132);
  569.  }
  570. }
  571.  
  572. /****************************************************************************/
  573. /****************************************************************************/
  574. /****************************************************************************/
  575.